#ifndef _MOTOR_ENCODER_H_
#define _MOTOR_ENCODER_H_

#include "main.h"
#include "tim.h"

//-----------------------------------------------	

typedef struct __Encoder_HandleTypeDef
{
  TIM_HandleTypeDef *htim;   /*!< 编码器使用的定时器  */
  uint32_t Channel;          /*!< 编码器通道         */
} Encoder_HandleTypeDef;

//-----------------------------------------------	


#define Encoder_GetCount(__hencoder__)              __HAL_TIM_GET_COUNTER((__hencoder__)->htim)               // 获取编码器计数值
#define Encoder_SetCount(__hencoder__, __count__)   __HAL_TIM_SET_COUNTER((__hencoder__)->htim, (__count__))  // 设置编码器计数值
#define Encoder_ResetCount(__hencoder__)            __HAL_TIM_SET_COUNTER((__hencoder__)->htim, 0)            // 重置编码器计数值
#define Encoder_GetDirection(__hencoder__)          (!__HAL_TIM_IS_TIM_COUNTING_DOWN((__hencoder__)->htim))   // 获取编码器计数方向  0: 计数下降  1: 计数上升


//-----------------------------------------------	

/**
 * @if Eng 
 * @brief Initializes a quadrature encoder interface using a timer 
 * @param hencoder - Pointer to Encoder_HandleTypeDef structure that holds encoder state 
 * @param htim - Pointer to TIM_HandleTypeDef structure for the timer peripheral 
 * @param Channel - Timer channel(s) to use for encoder interface (TIM_CHANNEL_x or TIM_CHANNEL_ALL)
 * @result Encoder interface is configured and ready to count pulses 
 * @else 
 * @brief 使用定时器初始化正交编码器接口
 * @param hencoder - 指向保存编码器状态的Encoder_HandleTypeDef结构的指针
 * @param htim - 指向定时器外设TIM_HandleTypeDef结构的指针 
 * @param Channel - 用于编码器接口的定时器通道(TIM_CHANNEL_x 或 TIM_CHANNEL_ALL)
 * @result 编码器接口配置完成并准备计数脉冲 
 * @endif 
 */ 
void Encoder_Init(Encoder_HandleTypeDef *hencoder, TIM_HandleTypeDef *htim, uint32_t Channel);


#endif
